/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.digitalpoint.dao;

import br.com.digitalpoint.Lotacao;
import br.com.digitalpoint.utils.FabricaConexao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author rodrigo
 */
public class LotacaoDAO
{
    public void incluir(Lotacao lotacao) throws Exception
    {
        try
        {
            String insere = "INSERT INTO TBLotacoes (matricula,cod_setor) VALUES (?,?)";
            Connection con = FabricaConexao.abreConexao();
            PreparedStatement ps = con.prepareStatement(insere);

            ps.setInt(1, lotacao.getMatricula());
            ps.setInt(2, lotacao.getCod_setor());
           
            ps.execute();
        }
        catch(Exception e)
        {
            throw new Exception("Lotação DAO: "+e.getMessage());
        }
        finally
        {
            FabricaConexao.fechaConexao();
        }
    }

    public void excluir(int matricula) throws Exception
    {
        try
        {
            String exclui = "DELETE FROM TBLotacoes WHERE matricula = ?";
            Connection con = FabricaConexao.abreConexao();
            PreparedStatement ps = con.prepareStatement(exclui);
            ps.setInt(1, matricula);
            ps.execute();
        }
        catch (SQLException e)
        {
            throw new Exception("LotacaoDAO: "+e.getMessage());
        }
        finally
        {
            FabricaConexao.fechaConexao();
        }
    }

    public void alterar(Lotacao lotacao) throws Exception
    {
        try
        {
            String altera = "UPDATE TBLotacoes SET cod_setor = ?  WHERE matricula = ?";
            Connection con = FabricaConexao.abreConexao();
            PreparedStatement ps = con.prepareStatement(altera);

            ps.setInt(1,lotacao.getCod_setor());
            ps.setInt(2,lotacao.getMatricula());
            ps.execute();
        }
        catch (SQLException e)
        {
            throw new Exception("LotacaorDAO: "+e.getMessage());
        }
        finally
        {
            FabricaConexao.fechaConexao();
        }
    }
}
